package com.example.pjy.service.impl;

import com.example.pjy.mapper.RulesMapper;
import com.example.pjy.mapper.Studentin2Mapper;
import com.example.pjy.mapper.Studentin3Mapper;
import com.example.pjy.mapper.TotalScoreMapper;
import com.example.pjy.pojo.Rules;
import com.example.pjy.pojo.Studentin2;
import com.example.pjy.pojo.Studentin3;
import com.example.pjy.service.Studentin3Service;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Studentin3ServiceImpl implements Studentin3Service {
    @Resource
    private Studentin3Mapper studentin3Mapper;

    @Resource
    private TotalScoreMapper totalScoreMapper;

    @Resource
    private RulesMapper rulesMapper;


    @Override
    public List<Studentin3> list(){
        return studentin3Mapper.list();
    }

    @Override
    public void updatest3(Studentin3 studentin3){
        studentin3Mapper.updatest3(studentin3);
    }

    @Override
    public void calTotalScore(Integer year){
        List<Studentin3> list = studentin3Mapper.list();//获取研二学生
        Rules rules = rulesMapper.findByYear(year);//获取年份规则
        System.out.println("获取到的规则：" + rules);
        for(Studentin3 st3:list){
            double total = st3.getPoliticalScore()*rules.getPoliticalScoreWeight()+
                    st3.getResearchScore()*rules.getResearchScoreWeight()+
                    st3.getSocialScore()*rules.getSocialScoreWeight();
            st3.setCalScore(total);//设置这个学生的计算成绩
            System.out.println("学生 " + st3.getStudentId() + " 的计算后成绩：" + total);
            studentin3Mapper.updatest3(st3);//更改这个学生
        }
    }





    @Override
    public void calAward(Integer year) {
        List<Studentin3> list = studentin3Mapper.list();
        Rules rules = rulesMapper.findByYear(year);
        int awardPeople = 0;
        for (Studentin3 st3 : list) {
            if (st3.getIsqualified() == 1 && awardPeople < rules.getAwardPeople()) {
                awardPeople++;
                st3.setAward(1);
                studentin3Mapper.updatest3(st3);
            }
            else{
                st3.setAward(0);
                studentin3Mapper.updatest3(st3);
            }
        }
    }
}
